﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Reflection;

namespace RLZY
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
        }
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string strErr = "";
                if (this.txtName.Text.Trim().Length == 0)
                {
                    strErr += "登录名不能为空！\r\n";
                }
                if (this.txtPass.Text.Trim().Length == 0)
                {
                    strErr += "密码不能为空！\r\n";
                }
                if (strErr != "")
                {
                    MessageBox.Show(this, strErr);
                    return;
                }
                SqlDataReader re = this.get(this.txtName.Text.Trim());
                if (re.Read())
                {
                    string pass = re["密码"].ToString();
                    if (pass == this.txtPass.Text.Trim())
                    {
                        others.UserHelper.UserID = re["id"].ToString();

                        this.Close();
                        //this.Dispose();
                    }
                    else { MessageBox.Show("密码错误。"); }
                }
                else { MessageBox.Show("未登记用户名"); }
            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        public SqlDataReader get(string name)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" select * from userpassword  where 用户名=@用户名   ");

            SqlParameter[] param = new SqlParameter[] 
            { 
                new SqlParameter("@用户名",name ),
            };

            return DbHelperSQL.ExecuteReader(strSql.ToString(), param);

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode);
            this.Dispose();
            Application.Exit();
        }
        //public string ID;
        

        //public string returnUserID()
        //{
        //    return this.ID;
        //    //return this.userName;
        //}
        
    }
}
